#include "requestinterceptor.h"
#include <QWebEngineView>
#include <QWebEngineSettings>
#include <QWebEngineProfile>
#include <QThread>
#include <QFile>
#include <QWebEngineUrlRequestInterceptor>
#include <QDebug>

RequestInterceptor::RequestInterceptor(QObject *parent)
    : QWebEngineUrlRequestInterceptor(parent)
   {
       QThread *thread = QThread::create([this]{
           QFile file(".\\easylistchina.txt");
//           QFile file("D:\\waterfox\\build-src-Desktop_Qt_6_2_4_MSVC2019_64bit-Release\\release\\easylistchina.txt");
           QString easyListTxt;

           if(!file.exists()) {
               qDebug() << "No easylistchina.txt file found.";
           } else {
               if (file.open(QIODevice::ReadOnly | QIODevice::Text)){
                   easyListTxt = file.readAll();
               }
               file.close();
               client.parse(easyListTxt.toStdString().c_str());
           }
       });
       thread->start();
   }

   void RequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
   {
       if (client.matches(info.requestUrl().toString().toStdString().c_str(),
           FONoFilterOption, info.requestUrl().host().toStdString().c_str())) {
               qDebug() << "Blocked: " << info.requestUrl();
               info.block(true);
       }
   }
